package com.egg.ylt.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.adapter.ADA_CallCenterAdapter;
import com.egg.ylt.pojo.CallCenterEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_CallCenter extends BaseActivity {

    @BindView(R.id.include_rl_view)
    RelativeLayout mIncludeRlView;

    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.recycler_call)
    RecyclerView recyclerCall;
    @BindView(R.id.btn_call)
    TextView btnCall;

    private ADA_CallCenterAdapter mCallAdapter;
    private List<CallCenterEntity> dataList;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_call_center;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);

        //设置title
        includeTvTitle.setText("客服中心");

        //获取本地json文件数据
        getJsonData();

    }

    private void getJsonData() {
        dataList = new ArrayList<>();
        //得到本地json文本内容
        try {
            InputStreamReader isr = new InputStreamReader(getAssets().open("CustormerService.json"), "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = br.readLine()) != null) {
                builder.append(line);
                Log.i("service", line);
            }
            br.close();
            isr.close();
            //gson转换的类型
            Type type = new TypeToken<List<CallCenterEntity>>() {
            }.getType();

            dataList = new Gson().fromJson(builder.toString(), type);
//            JSONArray testjson = new JSONArray(builder.toString());//builder读取了JSON中的数据。
////            //直接传入JSONObject来构造一个实例
////            JSONArray array = testjson.getJSONArray("role");         //从JSONObject中取出数组对象
//            for (int i = 0; i < testjson.length(); i++) {
//                JSONObject role = testjson.getJSONObject(i);    //取出数组中的对象
//                CallCenterEntity entity = new CallCenterEntity();
//                entity.setTitle(role.getString("title"));  //取出数组中对象的各个值
//                entity.setContent(role.getString("content"));
//                dataList.add(entity);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        //设置recyclerView
        initRecycler();
    }

    private void initRecycler() {
        //设置recyclerView布局管理器
        recyclerCall.setLayoutManager(new LinearLayoutManager(mContext));
        //设置适配器
        mCallAdapter = new ADA_CallCenterAdapter(mContext, dataList);
        recyclerCall.setAdapter(mCallAdapter);
        //item监听
        mCallAdapter.setOnItemClickListener(new ADA_CallCenterAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String title, String content) {
                Bundle mBundle = new Bundle();
                mBundle.putString("title", title);
                mBundle.putString("content", content);
                readyGo(ACT_CommonIssue.class, mBundle);
            }
        });
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }


    @OnClick({R.id.include_iv_back, R.id.btn_call})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.btn_call://联系平台客服
                CustomUtils.setCallPhoneDialog(this, Constants.companyServicePhone);
                break;
        }
    }
}
